{{- define "grafana_agent_config" }}
server:
  log_level: info
  http_listen_port: 8080
prometheus:
  wal_directory: /data/agent/wal
  global:
    scrape_interval: 5m
  configs:
  - name: agent
    host_filter: false
    max_wal_time: 360h
    scrape_configs:
    - job_name: 'deckhouse-telemetry'
      metrics_path: /metrics/hooks
      static_configs:
      - targets:
        - deckhouse.d8-system.svc.{{ .Values.global.discovery.clusterDomain }}:8080
      metric_relabel_configs:
      - source_labels: [__name__]
        regex: 'd8_telemetry_.+'
        action: keep
      - source_labels: [job]
        target_label: cluster_uuid
        replacement: {{ .Values.global.discovery.clusterUUID }}
      - source_labels: [instance]
        target_label: __replica__
        replacement: ${NODE_NAME}
      - regex: hook|instance
        action: labeldrop
    - job_name: 'flant-pricing'
      params:
        module: [http_2xx]  # Look for a HTTP 200 response.
      metrics_path: /metrics/hooks
      static_configs:
      - targets:
        - 127.0.0.1:9115
      metric_relabel_configs:
      - source_labels: [__name__]
        regex: 'flant_pricing_.+'
        action: keep
      - source_labels: [job]
        target_label: cluster_uuid
        replacement: {{ .Values.global.discovery.clusterUUID }}
      - source_labels: [instance]
        target_label: __replica__
        replacement: ${NODE_NAME}
      - regex: hook|instance
        action: labeldrop
    remote_write:
    - url: {{ .Values.flantIntegration.metrics.url }}
  {{- if dig "clusterConfiguration" "proxy" false .Values.global }}
    {{- $proxy := "" }}
    {{- if .Values.global.clusterConfiguration.proxy.httpProxy }}
      {{- $proxy = .Values.global.clusterConfiguration.proxy.httpProxy }}
    {{- end }}
    {{- if .Values.global.clusterConfiguration.proxy.httpsProxy }}
      {{- $proxy = .Values.global.clusterConfiguration.proxy.httpsProxy }}
    {{- end }}
    {{- if $proxy }}
      proxy_url: {{ $proxy }}
    {{- end }}
  {{- end }}
      bearer_token: {{ .Values.flantIntegration.internal.licenseKey }}
      metadata_config:
        send: false
{{- end }}

{{- if .Values.flantIntegration.metrics }}
---
apiVersion: v1
kind: Secret
metadata:
  name: grafana-agent-config
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "pricing")) | nindent 2 }}
data:
  agent-scraping-service.yaml: {{ include "grafana_agent_config" . | b64enc }}
{{- end }}
